package com.zhang.network.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.dto.BaseResponse;
import com.zhang.network.constant.BizResponse;
import com.zhang.network.mapper.UserMapper;
import com.zhang.network.model.*;
import com.zhang.network.model.friend.FriendQueryDTO;
import com.zhang.network.model.user.UserLoginDTO;
import com.zhang.network.model.user.UserQueryDTO;
import com.zhang.network.service.FriendService;
import com.zhang.network.service.NetworkUserService;
import com.zhang.network.service.UserService;
import com.zhang.util.ALiYun;
import com.zhang.util.OSSClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserMapper userMapper;

    @Resource
    UserService userService;

    @Resource
    FriendService friendService;

    @Resource
    NetworkUserService networkUserService;

    @GetMapping()
    public BaseResponse<User> test() {
        User users = userMapper.selectById(1);
        return BaseResponse.of("200", "success", users);
    }

    @PostMapping("/logout")
    public BaseResponse logout() {
        return BizResponse.success();
    }

    @GetMapping("/info")
    public BaseResponse getUserInfo(Integer token) {
        User user = userService.getById(token);
        log.info("得到用户信息");
//        user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return BaseResponse.of("200", "success", user);
    }

    @GetMapping("/data")
    public BaseResponse getUserData(Integer userId) {
        Map<String, String> map = new HashMap<>();
        List<Friend> list = friendService.list(new QueryWrapper<Friend>()
                .eq("user_id", userId));
        map.put("friendNum", list.size() + "");
        List<NetworkUser> networkList = networkUserService.list(new QueryWrapper<NetworkUser>()
                .eq("user_id", userId));
        map.put("networkNum", networkList.size() + "");
        return BizResponse.success(map);
    }

    @PostMapping("/login")
    public BaseResponse login(UserLoginDTO userLoginDTO) {
        log.info(userLoginDTO.toString());
        List<User> list = userService.list(new QueryWrapper<User>()
                .and(i -> i.eq("password", userLoginDTO.getPassword())
                        .eq("nick_name", userLoginDTO.getUsername()))
                .or(i -> i.eq("password", userLoginDTO.getPassword())
                        .eq("account", userLoginDTO.getUsername())));
        log.info("list长度" + list.size());
        if (list.size() > 0) {
            User user = list.get(0);
            return BizResponse.success(user);
        }
        return BizResponse.fail("账号密码错误");

    }

    @GetMapping("/register")
    public BaseResponse register(User user) {
        log.info("请求参数：{}", user.toString());
        Random random = new Random();
        long account = random.nextInt(10000) + System.currentTimeMillis();
        user.setAccount(account + "");
        boolean result = userService.save(user);
        if (result) {
            return BizResponse.success();
        }
        return BizResponse.fail("用户注册失败");
    }

    @GetMapping("/updateUser")
    public BaseResponse changePassword(User user) {
        log.info("请求参数：{}", user.toString());
        boolean result = userService.updateById(user);
        if (result) {
            return BizResponse.success();
        }
        return BizResponse.fail("修改失败");
    }

    @GetMapping("/page")
    public BaseResponse<Page<User>> page(UserQueryDTO queryDTO) {
        log.info("请求参数：{}", queryDTO.toString());
        Page<User> userPage = userService.pageUser(queryDTO);
        return BizResponse.success(userPage);
    }

    @GetMapping("/pageNoFriendUser")
    public BaseResponse<Page<User>> pageNoFriendUser(UserQueryDTO queryDTO) {
        log.info("请求参数：{}", queryDTO.toString());
        Page<User> userPage = userService.pageNoFriendUser(queryDTO);
        return BizResponse.success(userPage);
    }

    @RequestMapping("/uploadImg")
    public String uploadImg(MultipartFile file) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        String url = ossClientUtil.uploadImage(file);
        log.info(url);
        return url;
    }

}

